<h2>Why is this an issue?</h2>
<p>The common pattern of taking a class-file entity, obtaining a corresponding builder, examining each element of the entity and possibly replacing it
with other elements can be expressed by transforms. Using the <code>transformClass</code> method in that case instead of <code>build</code> removes
all that boilerplate and makes the intent clearer.</p>
<h2>How to fix it</h2>
<p>Use the <code>ClassFile.transformClass</code> method instead of <code>ClassFile.build</code>.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
public static void transformClassFile(Path path) throws IOException {
  ClassFile classFile = ClassFile.of();
  ClassModel classModel = classFile.parse(path);
  byte[] newBytes = classFile.build( // Noncompliant
    classModel.thisClass().asSymbol(), classBuilder -&gt; {
        for (ClassElement classElement : classModel) {
          if (!(classElement instanceof MethodModel methodModel &amp;&amp;
              methodModel.methodName().stringValue().startsWith("debug"))) {
            classBuilder.with(classElement);

          }
        }
    });
  Files.write(path, newBytes);
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
public static void transformClassFile(Path path) throws IOException {
  ClassFile classFile = ClassFile.of();
  ClassModel classModel = classFile.parse(path);
  byte[] newBytes = classFile.transformClass(
    classModel, (classBuilder, classElement) -&gt; {
      if (!(classElement instanceof MethodModel methodModel &amp;&amp;
            methodModel.methodName().stringValue().startsWith("debug"))) {
          classBuilder.with(classElement);
        }
      });
  Files.write(path, newBytes);
}
</pre>
<h2>Resources</h2>
<ul>
  <li> <a href="https://openjdk.org/jeps/484">JEP 484: Class-File API</a> </li>
  <li> <a
  href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/classfile/ClassFile.html#transformClass(java.lang.classfile.ClassModel,java.lang.classfile.ClassTransform)">ClassFile API</a> </li>
</ul>

